/* Original Author:  Dominick O'Dierno
 * Date: 2007   odiernod@gmail.com     --Originally intended for use with DOMercury:  http://www.odierno.com/domercury
 * Description: Handles the storing and distribution of icons.  Makes sure that duplicate icons are not stored.  Also cuts down on file I/O for retrieving multiple icons.
 * 
 * --CHANGELOG--
 * 
 * */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.IO;


namespace DOMercury.Managers
{
    public static class IconManager
    {
        private static Dictionary<string, Bitmap> iconsdict;

        private static Bitmap defaultIcon;
        
        public static void Initialize()
        {
            try
            {
                defaultIcon = (Bitmap)Bitmap.FromFile("Icons/default.png");
            }
            catch
            {
                //No default icon for you!
            }
            iconsdict = new Dictionary<string, Bitmap>();
            AddCustomIcon("Dive Arrow", (Bitmap)Bitmap.FromFile("Icons/arrow.png"));
            AddCustomIcon("Grep", (Bitmap)Bitmap.FromFile("Icons/dg.png"));
            AddCustomIcon("System Window", (Bitmap)Bitmap.FromFile("Icons/SystemWindow.png"));
            AddCustomIcon("The Current Foreground Window", System.Drawing.Icon.ExtractAssociatedIcon("Icons/SystemWindow.png").ToBitmap());
            AddCustomIcon("folder", (Bitmap)Bitmap.FromFile("Icons/folder.png"));
            AddCustomIcon("DOMercury Item", System.Drawing.Icon.ExtractAssociatedIcon("tray.ico").ToBitmap());
            AddCustomIcon(".url", Win32.ExtractShell32Icon(220).ToBitmap());
            AddCustomIcon("Path to My Computer",Win32.ExtractShell32Icon((int)Win32.Shell32Icons.Monitor).ToBitmap());
            AddCustomIcon("The Desktop", Win32.ExtractShell32Icon((int)Win32.Shell32Icons.Desktop).ToBitmap());
            AddCustomIcon("The ScreenSaver", Win32.ExtractShell32Icon((int)Win32.Shell32Icons.Sleep).ToBitmap());
            AddCustomIcon("Search Google", (Bitmap)Bitmap.FromFile("Icons/Google.png"));
            AddCustomIcon("text", (Bitmap)Bitmap.FromFile("Icons/Text.png"));
            AddActionIcons();


        }

        

        private static void AddActionIcons()
        {
            AddCustomIcon("Execute Command", Win32.ExtractShell32Icon(137).ToBitmap());
            AddCustomIcon("Execute", Win32.ExtractShell32Icon(137).ToBitmap());
            AddCustomIcon("Open in Internet Explorer", Win32.ExtractShell32Icon(220).ToBitmap());
            AddCustomIcon("Create Alias", Win32.ExtractShell32Icon(43).ToBitmap());
            AddCustomIcon("Launch", Win32.ExtractShell32Icon(39).ToBitmap());
            AddCustomIcon("Open", (Bitmap)Bitmap.FromFile("Icons/open.png"));
            AddCustomIcon("OpenWith", Win32.ExtractShell32Icon(155).ToBitmap());
            AddCustomIcon("Move", Win32.ExtractShell32Icon(68).ToBitmap());
            AddCustomIcon("Delete", Win32.ExtractShell32Icon(32).ToBitmap());
            AddCustomIcon("CopyTo", Win32.ExtractShell32Icon(134).ToBitmap());
            AddCustomIcon("Rename", Win32.ExtractShell32Icon(133).ToBitmap());
            AddCustomIcon("Send As Outlook Attachment", (Bitmap)Bitmap.FromFile("Icons/paperclip.png"));
            AddCustomIcon("Explore Parent Folder", (Bitmap)Bitmap.FromFile("Icons/folder.png"));
            AddCustomIcon("Restore", (Bitmap)Bitmap.FromFile("Icons/restore.png"));
            AddCustomIcon("Close", (Bitmap)Bitmap.FromFile("Icons/Close.png"));
            AddCustomIcon("Maximize", (Bitmap)Bitmap.FromFile("Icons/maximize.png"));
            AddCustomIcon("Minimize", (Bitmap)Bitmap.FromFile("Icons/minimize.png"));
            AddCustomIcon("Zip", (Bitmap)Bitmap.FromFile("Icons/zip.png"));
            AddCustomIcon("New Folder", (Bitmap)Bitmap.FromFile("Icons/newfolder.png"));
            AddCustomIcon("Search", (Bitmap)Bitmap.FromFile("Icons/google.png"));
            AddCustomIcon("Image Search", (Bitmap)Bitmap.FromFile("Icons/images.png"));
            AddCustomIcon("Map Search", (Bitmap)Bitmap.FromFile("Icons/maps.png"));
            AddCustomIcon("GMail Search", (Bitmap)Bitmap.FromFile("Icons/gmail.png"));
            AddCustomIcon("Product Search", (Bitmap)Bitmap.FromFile("Icons/products.png"));
            AddCustomIcon("Video Search", (Bitmap)Bitmap.FromFile("Icons/video.png"));
            AddCustomIcon("Open in Command Shell", (Bitmap)Bitmap.FromFile("Icons/cmd.png"));
            AddCustomIcon("Copy Path to Clipboard", (Bitmap)Bitmap.FromFile("Icons/paperclip.png"));

            //AddCustomIcon("Execute In Command Shell", Win32.ExtractShell32Icon(Win32.Shell32Icons.)

        }

        public static void AddCustomIcon(string key, Bitmap icon)
        {
            if (!iconsdict.ContainsKey(key))
            {
                try
                {
                    //Icon tmp = Icon.ExtractAssociatedIcon(filename);
                    iconsdict.Add(key, icon);
                }
                catch
                {
                    //Do Nothing
                }
            }
        }

        public static void AddIcon(string filename)
        {
            string key = GetKey(filename);
            if (!iconsdict.ContainsKey(key))
            {
                try
                {
                    if (PathFinder.IsPath(filename))
                    {
                        Icon tmp = Icon.ExtractAssociatedIcon(filename);
                        iconsdict.Add(key, tmp.ToBitmap());
                    }
                }
                catch
                {
                    //Do Nothing
                }
            }
        }

        public static Bitmap GetIconImage(string filename)
        {
            try
            {
                //return GetIcon(filename).ToBitmap();
                Bitmap tmp;
                iconsdict.TryGetValue(GetKey(filename), out tmp);
                if (tmp != null)
                {
                    return tmp;
                }
                else
                {
                    return defaultIcon;
                }
            }
            catch
            {
                return defaultIcon;
            }
        }
        //public static Icon GetIcon(string filename)
        //{
        //    Icon tmp;
        //    iconsdict.TryGetValue(GetKey(filename), tmp);
        //    return tmp;
        //}

        private static string GetKey(string filename)
        {
            if (Directory.Exists(filename))
            {
                return "folder";
            }
            else if (File.Exists(filename))
            {
                string ext = Path.GetExtension(filename).ToLower();
                if (ext == ".exe" || ext == ".lnk" || ext == ".ico" || ext == ".icn")
                {
                    return Path.GetFileName(filename);
                }
                else
                {
                    return ext;
                }
            }
            else return filename;
        }

        public static KeyValuePair<string, Bitmap> GetPairByIndex(int index)
        {
            int count = 0;
            foreach (KeyValuePair<string, Bitmap> entry in iconsdict)
            {
                if (count == index)
                {
                    return entry;
                }
                count++;
            }
            return new KeyValuePair<string, Bitmap>(); ;
        }
    }
}
